package com.jincp.common.popup;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.FloatRange;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.jincp.common.R;

/**
 * Created by jcp on 2017/6/23.
 */

public class PopupController {
    private int layoutResId;//布局id
    private Activity activity;
    private PopupWindow popupWindow;
    public View mPopupView;//弹窗布局View
    private View mView;
    private Window mWindow;

    PopupController(Activity activity, PopupWindow popupWindow) {
        this.activity = activity;
        this.popupWindow = popupWindow;
    }

    public void setView(int layoutResId) {
        mView = null;
        this.layoutResId = layoutResId;
        installContent();
    }

    public void setView(View view) {
        mView = view;
        this.layoutResId = 0;
        installContent();
    }

    private void installContent() {
        if (layoutResId != 0) {
            mPopupView = LayoutInflater.from(activity).inflate(layoutResId, null);
        } else if (mView != null) {
            mPopupView = mView;
        }
        popupWindow.setContentView(mPopupView);
    }

    /**
     * 设置宽度
     *
     * @param width  宽
     * @param height 高
     */
    public void setWidthAndHeight(int width, int height) {
        if (width == 0 || height == 0) {
            //如果没设置宽高，默认是WRAP_CONTENT
            popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
            popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        } else {
            popupWindow.setWidth(width);
            popupWindow.setHeight(height);
        }
    }


    /**
     * 设置背景灰色程度
     *
     * @param level 0.0f-1.0f
     */
    public void setBackGroundLevel(@FloatRange(from = 0.0, to = 1.0) float level) {
        mWindow = activity.getWindow();
        WindowManager.LayoutParams params = mWindow.getAttributes();
        params.alpha = level;
        mWindow.setAttributes(params);
    }


    /**
     * 设置动画
     */
    private void setAnimationStyle(int animationStyle) {
        popupWindow.setAnimationStyle(animationStyle);
    }

    /**
     * 设置Outside是否可点击
     *
     * @param touchable 是否可点击
     */
    private void setOutsideTouchable(boolean touchable) {
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));//设置透明背景
        popupWindow.setOutsideTouchable(touchable);//设置outside可点击
        popupWindow.setFocusable(touchable);
    }


    private void setPopupDismissListener(final PopupDismissListener dismissListener) {
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (dismissListener != null) {
                    dismissListener.dismiss(popupWindow);
                }
            }
        });
    }

    static class PopupParams {
        public int layoutResId;//布局id
        public Activity mActivity;
        public PopupDismissListener dismissListener;
        public int mWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
        public int mHeight= ViewGroup.LayoutParams.WRAP_CONTENT;//弹窗的宽和高
        public boolean isShowAnim = true;
        public int animationStyle = R.style.SlideBottomAnimation;//动画Id
        public View mView;
        public boolean isTouchable = true;

        public PopupParams(Activity activity) {
            this.mActivity = activity;
        }

        public void apply(PopupController controller) {
            if (mView != null) {
                controller.setView(mView);
            } else if (layoutResId != 0) {
                controller.setView(layoutResId);
            } else {
                throw new IllegalArgumentException("PopupView's contentView is null");
            }
            controller.setWidthAndHeight(mWidth, mHeight);
            controller.setPopupDismissListener(dismissListener);
            controller.setOutsideTouchable(isTouchable);//设置outside可点击
            if (isShowAnim) {
                controller.setAnimationStyle(animationStyle);
            }
        }
    }

}
